layui.use(['table', 'form'], function () {
    const table = layui.table,
        form = layui.form,
        $ = layui.jquery;

    // 创建渲染实例
    table.render({
        elem: '#ID-category-list',
        url: URL_SYSTEM_CATEGORY,
        toolbar: '#category-toolbar',
        defaultToolbar: ['filter', 'exports'],
        height: 'full-85',
        cellMinWidth: 80,
        page: true,
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'categoryCode', title: '分类编码', width: 220},
            {field: 'categoryName', title: '分类名称', width: 280, sort: true},
            {field: 'categoryDesc', title: '分类描述'},
            {fixed: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#category-bar'}
        ]],
        error: function (res, msg) {
            console.log(res, msg)
        }
    });

    // 触发表格复选框选择
    table.on('checkbox(category-list)', function (obj) {
        console.log(obj)
    });

    // 触发表格单选框选择
    table.on('radio(category-list)', function (obj) {
        console.log(obj)
    });

    // 触发单元格工具事件
    table.on('tool(category-list)', function (obj) {
        const data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                id: 'layer-category',
                type: 2,
                area: ['100%', '100%'],
                title: '编辑 - id:' + data.categoryCode,
                content: 'category-edit.html',
                success: function (layero, index) {
                    let body = layer.getChildFrame('body', index);
                    body.find('#code').val(data.categoryCode);
                }
            });
        }
        if (obj.event === 'delete') {
            layer.confirm('确定删除' + data.categoryCode + '吗?', {icon: 3, title: '警告'}, function (index) {
                sendDeleteRequest(data.categoryCode);
                layer.close(index);
            });
        }
    });

    form.on('submit(category-list-search)', function(data){
        let categoryCode = data.field.categoryCode;
        let categoryName = data.field.categoryName;
        table.reload(TABLE_ID_CATEGORY, {
            url: URL_SYSTEM_CATEGORY + '?categoryCode=' + categoryCode + '&categoryName=' + categoryName
        });
        return false;
    });

    function sendDeleteRequest(categoryCode) {
        $.ajax({
            type: 'delete',
            url: URL_SYSTEM_CATEGORY + '/' + categoryCode,
            headers: COMMON_HEADERS,
            success: function (res) {
                if (res.code === 0) {
                    table.reload(TABLE_ID_CATEGORY, {
                        url: URL_SYSTEM_CATEGORY
                    });
                    layer.msg(OPERATION_SUCCESS.MESSAGE, {
                        time: 2000
                    });
                } else {
                    layer.alert(JSON.stringify(res), OPERATION_SUCCESS.ICON_CONTENT);
                }
            },
            error: function (res) {
                layer.alert(JSON.stringify(res), OPERATION_SUCCESS.ICON_CONTENT);
            }
        });
    }
});